package com.migu.contacts.login;

import static com.migu.contacts.net.Service.API;

import android.content.Intent;
import android.util.Log;
import android.view.View;

import com.blankj.utilcode.util.ToastUtils;
import com.frank.libandroid.BaseActivity;
import com.frank.libandroid.HttpCallback;
import com.frank.libandroid.util.AutoDeleteUtil;
import com.frank.libandroid.util.PasswordVisibleUtil;
import com.frank.libandroid.util.TextWatcherUtil;
import com.migu.contacts.BuildConfig;
import com.migu.contacts.MainActivity;
import com.migu.contacts.bean.LoginReq;
import com.migu.contacts.bean.LoginRes;
import com.migu.contacts.databinding.ActivityLoginPasswordBinding;
import com.migu.contacts.main.my.PrivacyActivity;
import com.migu.contacts.main.my.UserProtocolActivity;
import com.migu.contacts.util.UserCenter;

public class PasswordLoginActivity extends BaseActivity<ActivityLoginPasswordBinding> {
    public void goPrivacy(View view) {
        start(PrivacyActivity.class);
    }

    public void goProtocol(View view) {
        start(UserProtocolActivity.class);
    }

    @Override
    protected void initView() {
        super.initView();
        AutoDeleteUtil.autoDelete(mBinding.phone, mBinding.phoneDelete);
        TextWatcherUtil.setTextWatchEnable(mBinding.btn, mBinding.phone, mBinding.codeInput);
        PasswordVisibleUtil.setPasswordVisible(mBinding.codeInput, mBinding.showPwd);
    }

    public void switchStatus(View view) {
        view.setSelected(!view.isSelected());
    }

    @Override
    protected void initData() {
        super.initData();
        LoginReq bean = new LoginReq();
        bean.grantType = "password";
        if (BuildConfig.DEBUG) {
            bean.username = "13938260436";
            bean.password = "111111";
        }
        mBinding.setBean(bean);
    }

    public void login(View view) {
        Log.d(TAG, "login() called with: view = [" + view + "]");
        if (!mBinding.check.isSelected()) {
            ToastUtils.showShort("请阅读平台协议");
            return;
        }
        API.login(mBinding.getBean()).watch(this, new HttpCallback<LoginRes>() {
            @Override
            protected void onSuccess(LoginRes loginRes) {
                UserCenter.setLoginRes(loginRes);
                finish();
                Intent intent = new Intent(mActivity, MainActivity.class);
                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
                startActivity(intent);
            }
        });
    }


    public void passwordLogin(View view) {
        start(LoginActivity.class);
    }

    public void register(View view) {
        start(ForgetPwdActivity.class);
    }

    public void wechat(View view) {
    }

    public void qq(View view) {
    }

    public void weibo(View view) {
    }

    public void back(View view) {
        finish();
    }
}